package com.sojson.project.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sojson.constant.Lock;
import com.sojson.project.dao.GoodSpecsDao;
import com.sojson.project.dao.SpecsCateDao;
import com.sojson.project.entity.po.GoodSpecs;
import com.sojson.project.service.GoodSpecsService;
import com.sojson.util.ExpUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.file.constant.ConstantFileFieldName;
import com.sojson.util.file.depot.FileDepotUtil;
import com.sojson.util.file.enums.EFileTable;

/**
 * 商品规格组合表Service实现类
 * 
 * @author liu
 * @date 2021-03-22
 */
@Service
public class GoodSpecsServiceImpl implements GoodSpecsService {

    @Resource
    private GoodSpecsDao goodSpecsDaoImpl;
    @Resource
    private SpecsCateDao specsCateDaoImpl;

    /**
     * 单条插入
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int insert(GoodSpecs goodSpecs) {
        List<GoodSpecs> goodSpecss = goodSpecs.getGoodSpecss();

        // 添加的条目数
        int insert = 0;
        // 统计商品规格组合列表的数量
        int countByDto = 0;
        // 商品规格组合列表
        GoodSpecs gsPam = new GoodSpecs();
        for (GoodSpecs gs : goodSpecss) {
            synchronized (Lock.GOOD_SPECS) {
                // 检查此规格列表是否存在
                gsPam.setGoodId(gs.getGoodId());
                gsPam.setSpecsListIds(gs.getSpecsListIds());
                countByDto = goodSpecsDaoImpl.countByDto(gsPam);
                // 存在就结束
                if (countByDto > 0) {
                    continue;
                }

                insert = goodSpecsDaoImpl.insert(gs);
            }

            if (insert > 0) {
                FileDepotUtil.insertSync(EFileTable.GOOD_SPECS, ConstantFileFieldName.THUMB, gs.getThumb());
            }
        }
        return insert;
    }

    /**
     * 单条插入
     * 
     * @param goodSpecs
     * @return
     */
    @Override
    public int insertOne(GoodSpecs goodSpecs) {
        // 添加的条目数
        int insert = goodSpecsDaoImpl.insert(goodSpecs);
        if (insert > 0) {
            FileDepotUtil.insertSync(EFileTable.GOOD_SPECS, ConstantFileFieldName.THUMB, goodSpecs.getThumb());
        }
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public int insertList(GoodSpecs goodSpecs) {
        int insertList = goodSpecsDaoImpl.insertList(goodSpecs.getGoodSpecss());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByDto(GoodSpecs goodSpecs) {
        int deleteByDto = goodSpecsDaoImpl.deleteByDto(goodSpecs);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public int countAll() {
        int countAll = goodSpecsDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public List<GoodSpecs> findAll() {
        List<GoodSpecs> findAll = goodSpecsDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public int countByDto(GoodSpecs goodSpecs) {
        int countByDto = goodSpecsDaoImpl.countByDto(goodSpecs);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public List<GoodSpecs> findByDto(GoodSpecs goodSpecs) {
        List<GoodSpecs> findByDto = goodSpecsDaoImpl.listByDto(goodSpecs);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = goodSpecsDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByIdList(List<Integer> ids) {
        List<GoodSpecs> findByIdList = goodSpecsDaoImpl.findByIdList(ids);

        int deleteByIdList = goodSpecsDaoImpl.deleteByIdList(ids);

        if (deleteByIdList > 0) {
            List<String> orderNoList = findByIdList.stream().map(GoodSpecs::getThumb).collect(Collectors.toList());
            FileDepotUtil.deleteListSync(orderNoList);
        }
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public int updateById(GoodSpecs goodSpecs) {
        int updateById = goodSpecsDaoImpl.updateById(goodSpecs);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticByIdList(GoodSpecs goodSpecs) {
        int updateStaticByIdList = goodSpecsDaoImpl.updateStaticByIdList(goodSpecs);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangeByIdList(GoodSpecs goodSpecs) {
        int updateChangeByIdList = goodSpecsDaoImpl.updateChangeByIdList(goodSpecs.getGoodSpecss());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public int updatePartialById(GoodSpecs goodSpecs) {
        GoodSpecs findById = goodSpecsDaoImpl.findById(goodSpecs.getId());

        int updatePartialById = goodSpecsDaoImpl.updatePartialById(goodSpecs);

        if (updatePartialById > 0) {
            FileDepotUtil.updateSync(EFileTable.GOOD_SPECS.getCode(), ConstantFileFieldName.THUMB, goodSpecs.getThumb(),
                findById.getThumb());
        }
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticPartialByIdList(GoodSpecs goodSpecs) {
        int updateStaticPartialByIdList = goodSpecsDaoImpl.updateStaticPartialByIdList(goodSpecs);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangePartialByIdList(GoodSpecs goodSpecs) {
        int updateChangePartialByIdList = goodSpecsDaoImpl.updateChangePartialByIdList(goodSpecs.getGoodSpecss());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public GoodSpecs findById(String id) {
        GoodSpecs findById = goodSpecsDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<GoodSpecs> findByIdList(ArrayList<Integer> ids) {
        ArrayList<GoodSpecs> findByIdList = goodSpecsDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Integer> findIdByDto(GoodSpecs goodSpecs) {
        ArrayList<Integer> findIdByDto = goodSpecsDaoImpl.findIdByDto(goodSpecs);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     * 
     * @throws SQLException
     */
    @Override
    public void clear() {
        goodSpecsDaoImpl.clear();
    }

    /**
     * 根据对象查询(包含规格详细)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @Override
    public List<GoodSpecs> list(GoodSpecs goodSpecs) {
        return goodSpecsDaoImpl.list(goodSpecs);
    }

    /**
     * 通过id查询(包含规格详细)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public GoodSpecs getInfo(Integer id) {
        return goodSpecsDaoImpl.getInfo(id);
    }

    /**
     * 添加库存
     * 
     * @param goodSpecs
     * @return
     */
    @Override
    public int updateStock(GoodSpecs goodSpecs) {
        int updatePartialById = 0;

        GoodSpecs goodSpe = new GoodSpecs();
        goodSpe.setId(goodSpecs.getId());

        synchronized (Lock.GOOD_STOCK) {
            GoodSpecs findById = goodSpecsDaoImpl.findById(goodSpecs.getId());
            if (StringUtil.isBlankObject(findById)) {
                return 1;
            }

            goodSpe.setStock(goodSpecs.getStock() + findById.getStock());
            updatePartialById = goodSpecsDaoImpl.updatePartialById(goodSpe);
        }
        return updatePartialById;
    }

    /**
     * 添加库存
     * 
     * @param goodSpecs
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int updateStock(List<GoodSpecs> goodSpecss) {
        int updatePartialById = 0;
        synchronized (Lock.GOOD_STOCK) {
            for (GoodSpecs goodSpecs : goodSpecss) {
                updatePartialById = goodSpecsDaoImpl.updateStock(goodSpecs);
                if (updatePartialById < 1) {
                    ExpUtil.throwEx("修改商品库存失败！");
                }
            }
        }
        return updatePartialById;
    }

    /**
     * 统计规格组合中使用此规格的数量
     * 
     * @param id
     * @return
     */
    @Override
    public int countBySpecsListId(Integer id) {
        return goodSpecsDaoImpl.countBySpecsListId(id);
    }

}